﻿@using PlatformNetCore.Entity
@{
    ViewBag.Title = "角色列表";
}

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    @if (ViewBag.right_actions != null)
                    {
                        <div style="padding-bottom: 10px;">
                            @foreach (SysRight o in ViewBag.right_actions)
                            {
                                <button class="layui-btn layui-btn-sm layuiadmin-btn-useradmin @o.SubSystem" data-type="@o.ActionType.ToLower()" curl="@o.MenuUrl" ctitle="@o.DisplayName">@o.DisplayName</button>
                            }
                        </div>
                    }

                    <table class="layui-hide" id="rolelist-table" lay-filter="rolelist-table"></table>

                    <script type="text/html" id="rolelist-operation">
                        @if (ViewBag.table_actions != null && ViewBag.table_actions.Count > 0)
                        {
                            @foreach (SysRight a in ViewBag.table_actions)
                            {
                                <button class="layui-btn layui-btn-xs layuiadmin-btn-useradmin @a.SubSystem" lay-event="@a.ActionType.ToLower()" curl="@a.MenuUrl" ctitle="@a.DisplayName"><i class=" @a.Pic"></i>@a.DisplayName</button>
                            }
                        }
                    </script>

                </div>

            </div>
        </div>
    </div>
</div>
<script type="text/html" id="parentTpl">
    {{#  if(d.SysRole_Parent != null){ }}
    <span>{{d.SysRole_Parent.RoleName}}</span>
    {{#  } else { }}
    <span>无</span>
    {{#  } }}
</script>
<script>
    //这里设置一个空的json串，用于接受表格的行信息
    var editData;
    var tree;

    layui.use('table', function () {
        var $ = layui.$, form = layui.form, table = layui.table;
        var option = {
            elem: '#rolelist-table'
            , url: '/Admin/Sys/RoleList'
            , method: 'POST'
            , page: true //开启分页
            , request: {
                pageName: 'pageindex',
                limitName: 'pagesize'
            }
            , limit: 10
            , limits: [10, 20, 50]
            , cols: [[
                { field: 'ID', title: '角色编号' }
                , { field: 'RoleName', title: '角色名称' }
                , { field: 'SysRole_Parent', title: '上级角色', templet: '#parentTpl' }
                , { field: 'SortNo', title: '排序号' }
                , { field: 'AddBy', title: '添加人' }
                , { field: 'AddDate', title: '添加时间' }
                , { align: 'center', fixed: 'right', title: '操作', toolbar: '#rolelist-operation' }
            ]]
        };
        //方法级渲染
        table.render(option);

        //监听行工具事件
        table.on('tool(rolelist-table)', function (obj) {
            var data = obj.data;
            console.log(obj)

            if (obj.event === 'delete') {

                //询问框
                layer.confirm('确认删除吗？', {
                    btn: ['确定', '关闭'], //按钮
                    title: '提示'
                }, function () {

                    var loadingFlag;
                    $.ajax({
                        url: "/Admin/Sys/RoleDel",
                        type: "POST",
                        data: { id: data.ID },
                        beforeSend: function () {
                            //注意，layer.msg默认3秒自动关闭，如果数据加载耗时比较长，需要设置time
                            loadingFlag = layer.msg('正在处理，请稍候……', { icon: 16, shade: 0.01, shadeClose: false, time: 10000 });
                        },
                        success: function (response) {

                            layer.close(loadingFlag);

                            if (response.success) {
                                obj.del();
                                //提交 Ajax 成功后，静态更新表格中的数据
                                layer.msg(response.msg, { icon: 1, time: 1500 }, function () {
                                    table.reload('rolelist-table'); //数据刷新
                                });

                            } else {

                                layer.msg(response.msg, { icon: 2, time: 1500 });

                            }
                        },
                        complete: function () {

                        }
                    });

                }, function () {

                });

            } else if (obj.event === 'edit') {

                console.log(data)

                //这行是监听到的表格行数据信息，复制给json全局变量。
                editData = data;

                var url = $(this).curl || $(this).attr("curl");
                var title = $(this).ctitle || $(this).attr("ctitle");

                layer.open({
                    type: 2
                    , title: title
                    , content: url + '?id=' + data.ID
                    , maxmin: true
                    , area: ['550px', '450px']
                    , btn: ['保存', '关闭']
                    , yes: function (index, layero) {

                        var iframeWindow = window['layui-layer-iframe' + index];
                        var submitID = 'roleedit-submit';
                        var submit = layero.find('iframe').contents().find('#' + submitID);

                        console.log(submit)

                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段

                            console.log(field);

                            var loadingFlag;

                            $.ajax({
                                url: "/Admin/Sys/RoleEdit",
                                type: "POST",
                                data: data.field,
                                beforeSend: function () {
                                    //注意，layer.msg默认3秒自动关闭，如果数据加载耗时比较长，需要设置time
                                    loadingFlag = layer.msg('正在保存，请稍候……', { icon: 16, shade: 0.01, shadeClose: false, time: 10000 });
                                },
                                success: function (response) {

                                    layer.close(loadingFlag);

                                    if (response.success) {

                                        //提交 Ajax 成功后，静态更新表格中的数据
                                        layer.msg('保存成功', { icon: 1, time: 1500 }, function () {
                                            table.reload('rolelist-table'); //数据刷新
                                            layer.close(index); //关闭弹层
                                        });

                                    } else {

                                        layer.msg(response.msg, { icon: 2, time: 1500 });

                                    }
                                },
                                complete: function () {

                                }
                            });

                        });

                        submit.trigger('click');
                    }
                });

            }
            else if (obj.event == "setting") {

                var url = $(this).curl || $(this).attr("curl");
                var title = $(this).ctitle || $(this).attr("ctitle");

                layer.open({
                    type: 2
                    , title: title
                    , content: url + '?id=' + data.ID
                    , maxmin: true
                    , area: ['700px', '90%']
                    , btn: ['保存', '关闭']
                    , yes: function (index, layero) {

                        var checkedData = tree.getChecked('treeId'); //获取选中节点的数据

                        console.log(checkedData);

                        var loadingFlag;

                        $.ajax({
                            url: "/Admin/Sys/RoleSetup",
                            type: "POST",
                            data: { id: data.ID, treeList: checkedData },
                            beforeSend: function () {
                                //注意，layer.msg默认3秒自动关闭，如果数据加载耗时比较长，需要设置time
                                loadingFlag = layer.msg('正在保存，请稍候……', { icon: 16, shade: 0.01, shadeClose: false, time: 10000 });
                            },
                            success: function (response) {

                                layer.close(loadingFlag);

                                if (response.success) {

                                    //提交 Ajax 成功后，静态更新表格中的数据
                                    layer.msg('保存成功', { icon: 1, time: 1500 }, function () {
                                        layer.close(index); //关闭弹层
                                        if (response.data) {
                                            parent.window.location.href = "@Url.Action("Index","Account")";
                                        }
                                    });

                                } else {
                                    layer.msg(response.msg, { icon: 2, time: 1500 });
                                }
                            },
                            complete: function () {

                            }
                        });

                    }
                });


            }
        });

        //事件
        var active = {
            //新增
            add: function () {
                var url = $(this).curl || $(this).attr("curl");
                var title = $(this).ctitle || $(this).attr("ctitle");

                layer.open({
                    type: 2
                    , title: title
                    , content: url
                    , maxmin: true
                    , area: ['550px', '450px']
                    , btn: ['保存', '关闭']
                    , yes: function (index, layero) {

                        var iframeWindow = window['layui-layer-iframe' + index];
                        var submitID = 'roleadd-submit';
                        var submit = layero.find('iframe').contents().find('#' + submitID);

                        console.log(submit)

                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段

                            console.log(field);

                            var loadingFlag;

                            $.ajax({
                                url: "/Admin/Sys/RoleAdd",
                                type: "POST",
                                data: data.field,
                                beforeSend: function () {
                                    //注意，layer.msg默认3秒自动关闭，如果数据加载耗时比较长，需要设置time
                                    loadingFlag = layer.msg('正在保存，请稍候……', { icon: 16, shade: 0.01, shadeClose: false, time: 10000 });
                                },
                                success: function (response) {

                                    layer.close(loadingFlag);

                                    if (response.success) {

                                        //提交 Ajax 成功后，静态更新表格中的数据
                                        layer.msg('保存成功', { icon: 1, time: 1500 }, function () {
                                            table.reload('rolelist-table'); //数据刷新
                                            layer.close(index); //关闭弹层
                                        });

                                    } else {

                                        layer.msg(response.msg, { icon: 2, time: 1500 });

                                    }
                                },
                                complete: function () {

                                }
                            });

                        });

                        submit.trigger('click');
                    }
                });

            }

        };

        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });
</script>
